package by.grsu.coctails.db.task;

import java.util.List;

import android.content.Context;
import by.grsu.coctails.model.Coctail;

public abstract class GetCoctailsAsyncTask extends CommonGetAsyncTask<Coctail> {

	private String categoryName;

	public GetCoctailsAsyncTask(Context context, String categoryName) {
		super(context);
		this.categoryName = categoryName;
	}

	@Override
	protected List<Coctail> load() {
		if (categoryName != null && !categoryName.equals("")) {
			return getDbManager().getCoctailsByCategory(categoryName);
		}
		return getDbManager().getAllCoctails();
	}

	@Override
	public List<Coctail> onPrepareResult(List<Coctail> result) {
		StringBuilder stringBuilder = new StringBuilder();
		for (int j = 0; j < result.size(); j++) {
			for (int i = 0; i < result.get(j).getIngredients().size(); i++) {
				stringBuilder.append(result.get(j).getIngredients().get(i)
						.getName());
				if (i + 1 < result.get(j).getIngredients().size()) {
					stringBuilder.append(", ");
				}
			}

			result.get(j).setCompostionString(stringBuilder.toString());
			stringBuilder = new StringBuilder();
		}

		return super.onPrepareResult(result);
	}

	public String getCategoryName() {
		return categoryName;
	}

}
